package com.twu.biblioteca;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/20/13
 * Time: 8:24 AM
 * To change this template use File | Settings | File Templates.
 */
public class Movie {
    private final String name;
    private final String year;
    private final String director;
    private float rating;

    public static final String NON_RATING = "N/A";
    public Movie(String name, String year, String director) {

        this.name = name;
        this.year = year;
        this.director = director;
        this.rating = -1.0f;
    }

    public String toString(){
        int titleWidth = 20;
        int yearWidth = 8;
        int directorWidth = 20;
        StringBuilder builder = new StringBuilder();
        builder.append(name);
        if (name.length() > titleWidth){
            builder.setLength(titleWidth);
        }
        else {
            for (int i = 0; i < titleWidth - name.length(); ++i){
                builder.append(" ");
            }
        }

        builder.append(year);
        if (year.length() > yearWidth){
            builder.setLength(yearWidth + titleWidth);
        }
        else {
            for (int i = 0; i < yearWidth - year.length(); ++i){
                builder.append(" ");
            }
        }

        builder.append(director);
        if (director.length() > directorWidth){
            builder.setLength(titleWidth + yearWidth + directorWidth);
        }
        else {
            for (int i = 0; i < directorWidth - director.length(); ++i){
                builder.append(" ");
            }
        }

        builder.append(ratingAppearance());
        return builder.toString();
    }

    private boolean hasRating() {
        return rating >= 0;
    }

    private String ratingAppearance(){
        if (hasRating()){
            return String.valueOf(rating);
        }
        return NON_RATING;
    }
}
